package unit23.warehouse;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sun.media.sound.RIFFInvalidDataException;

public class WareHouseImpl extends UnicastRemoteObject implements WareHouse{
	private Map<String , Double> prices;
	private Map<String , Product> product;
	private WareHouse backup;
	
	
	protected WareHouseImpl(WareHouse backup) throws RemoteException {
		product=new HashMap<>();
		this.backup=backup;
		prices=new HashMap<String, Double>();
		prices.put("Black", 234.345);
		prices.put("Mike", 2367.56);
	}
	public void add(String keyword,Product product){
		product.setLocation(this);
		this.product.put(keyword, product);
		
	}
	@Override
	public double getPrice(String description) throws RemoteException {
		// TODO Auto-generated method stub
		for(Product p:product.values())
			if(p.getDescriptio().equals(description)){
				return p.getPrice();
			}
		if(backup==null)
			return 0;
		else
			return backup.getPrice(description);
	}

	@Override
	public Product getProduct(List<String> keyWords) throws RemoteException {
		// TODO Auto-generated method stub
		for(String keyword:keyWords){
			Product p=product.get(keyword);
			if(p!=null)
				return p;
		}
		if(backup!=null){
			return backup.getProduct(keyWords);
		}
		else if(product.values().size()>0)
			return 	product.values().iterator().next();
		else
			return null;
	}
	
}
